
var itstock_lightbox_id = 0;

function itstock_lightbox() {
    var lb_id = itstock_lightbox_id;
    itstock_lightbox_id++;
    var div_blackbackground = CE("div");
    div_blackbackground.id = "blackbackground" + lb_id;
    div_blackbackground.style.display = 'block';
    A(div_blackbackground, 'class', 'blackbackground');
    AC(E('container'), div_blackbackground);
    var div_lightbox = CE("div");
    div_lightbox.id = "lightbox" + lb_id;
    A(div_lightbox, 'class', 'lightbox');
    AC(E('container'), div_lightbox);
    div_lightbox.style.display = 'inline-block';
    var div_close = CE("div");
    A(div_close, "class", "close");
    A(div_close, "onclick", "itstock_lightbox_close(" + lb_id + ");");
    AC(div_lightbox, div_close);
    var img_close = CE("img");
    img_close.src = "img/close.png";
    A(img_close, "alt", "x");
    A(img_close, "tittle", "Close");
    AC(div_close, img_close);
    var div_scrolable = CE("div");
    A(div_scrolable, "class", "scrolable");
    div_scrolable.id = "scrolable" + lb_id;
    AC(div_lightbox, div_scrolable);
    return lb_id;
}

function itstock_lightbox_get_id(internal_elm){
  while(internal_elm !== document && A(internal_elm, 'class') !== 'scrolable') {
    internal_elm = internal_elm.parentNode;
  }
  if(internal_elm === document){
    return undefined;
  }
  return internal_elm.id.replace('scrolable', '');
}

function itstock_lightbox_close(lb_id, elm) {
    if(lb_id === null) {
      lb_id = itstock_lightbox_get_id(elm);
    }
    E('blackbackground' + lb_id).style.display = 'none';
    E('lightbox' + lb_id).style.display = 'none';
    RC(E('container'), E('blackbackground' + lb_id));
    RC(E('container'), E('lightbox' + lb_id));
}

function itstock_request_popup(page, post, width, height, headers, lb_id){
  if(lb_id === undefined){
    var lb_id = itstock_lightbox();
    A(E('lightbox' + lb_id), 'style', A(E('lightbox' + lb_id), 'style') + " width: " + width  + "px; height: " + height + "px; margin-top: -" + (height/2 + 25) + "px; margin-left: -" + width/2 + "px;");
  }
  return itstock_request(page, post, headers, 'scrolable' + lb_id);
}